﻿using GalaSoft.MvvmLight;
using System.Collections.ObjectModel;

namespace ASoft.WPF.ViewModel
{
    /// <summary>
    /// This class contains properties that a View can data bind to.
    /// <para>
    /// Use the <strong>mvvminpc</strong> snippet to add bindable properties to this ViewModel.
    /// </para>
    /// <para>
    /// You can also use Blend to data bind with the tool's support.
    /// </para>
    /// <para>
    /// See http://www.galasoft.ch/mvvm/getstarted
    /// </para>
    /// </summary>
    public class TreeItemViewModel : ViewModelBase
    {
        internal TreeItemViewModel(TreeViewModel tree)
        {
            this.Tree = tree;
        }
        #region IsSelected

        /// <summary>
        /// The <see cref="IsSelected" /> property's name.
        /// </summary>
        public const string IsSelectedPropertyName = "IsSelected";

        private bool _IsSelected = false;

        /// <summary>
        /// Gets the IsSelected property.
        /// Changes to that property's value raise the PropertyChanged event. 
        /// This property's value is broadcasted by the Messenger's default instance when it changes.
        /// </summary>
        public bool IsSelected
        {
            get
            {
                return _IsSelected;
            }

            set
            {
                if (_IsSelected == value)
                {
                    return;
                }

                var oldValue = _IsSelected;
                _IsSelected = value;
                RaisePropertyChanged(IsSelectedPropertyName);
                Tree.ReloadSelectedItems();
                if (Tree.IsChangedChild)
                {
                    foreach (var item in Items)
                    {
                        item.IsSelected = _IsSelected;
                    }
                }
            }
        }

        #endregion

        #region Name

        /// <summary>
        /// The <see cref="Name" /> property's name.
        /// </summary>
        public const string NamePropertyName = "Name";

        private string _Name = string.Empty;

        /// <summary>
        /// Gets the Name property.
        /// Changes to that property's value raise the PropertyChanged event. 
        /// This property's value is broadcasted by the Messenger's default instance when it changes.
        /// </summary>
        public string Name
        {
            get
            {
                return _Name;
            }

            set
            {
                if (_Name == value)
                {
                    return;
                }

                var oldValue = _Name;
                _Name = value;

                RaisePropertyChanged(NamePropertyName);

            }
        }

        #endregion

        #region Tag

        /// <summary>
        /// The <see cref="Tag" /> property's name.
        /// </summary>
        public const string TagPropertyName = "Tag";

        private object _Tag = null;

        /// <summary>
        /// Gets the Tag property.
        /// Changes to that property's value raise the PropertyChanged event. 
        /// This property's value is broadcasted by the Messenger's default instance when it changes.
        /// </summary>
        public object Tag
        {
            get
            {
                return _Tag;
            }

            set
            {
                if (_Tag == value)
                {
                    return;
                }

                var oldValue = _Tag;
                _Tag = value;
                if (_Tag == null)
                {
                    Name = "";
                }
                else
                {
                    Name = _Tag.ToString();
                }
                RaisePropertyChanged(TagPropertyName);
            }
        }

        #endregion

        #region Items

        ObservableCollection<TreeItemViewModel> _Items;
        public ObservableCollection<TreeItemViewModel> Items
        {
            get
            {
                if (_Items == null)
                {
                    _Items = new ObservableCollection<TreeItemViewModel>();
                }
                return _Items;
            }
        }

        #endregion

        public TreeViewModel Tree { get; private set; }

    }
}